package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.User;
import com.study.services.CollectionService;
import com.study.services.CustomerService;
import com.study.services.DictionaryService;
import com.study.services.UserService;

@Controller
public class ColleController {
	@Autowired
	CollectionService service;
	@Autowired
	UserService userservice;
	@Autowired
	DictionaryService dicservice;
	@Autowired
	CustomerService cservice;


	/**
	 * 第一次进入界面，ajax加载全部数据 ty
	 */
	@PostMapping("getCustomer/huokea")
	@ResponseBody
	public String getForm1() {
		List<JSONObject> list = service.selectBySel(1, 10, "", "");
         //pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
         PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
         String json = JSONObject.toJSONString(pager);
		return json;
	}

	/**
	 * 加载“新建”的iframe界面 ty
	 * 
	 * @return
	 */
	@GetMapping("huoke2")
	public ModelAndView getForm2() {
		ModelAndView mv = new ModelAndView("/getCustomer/addcrmindex");
		return mv;
	}

	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("getCustomer/huokexiansuo")
	@ResponseBody
	public String getForm3(String input, String selects, int currentPage, int pagesize) {
		
		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(), selects.trim());
		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		
		
        String json = JSONObject.toJSONString(pager);
        
        
		return json;
	}

	/**
	 * 添加采集信息 ty
	 * 
	 * @param cusfrom
	 * @param company
	 * @param linkman
	 * @param tel
	 * @param demend
	 * @param more
	 * @return
	 */
	@PostMapping("form")
	@ResponseBody
	public ModelAndView handleForm(String cusfrom, String company, String linkman, String tel, String demend,
			String more,Integer radio,HttpSession session ) {
		ModelAndView mv = new ModelAndView("/error");
		String username = (String) session.getAttribute("username");
		User user = null;
		if (radio == 2) {//不是公海
			user = userservice.queryByName(username);
		}
		// 采集表的分类
		Dictionary dictionary1 = dicservice.findByDicId(1721);
		// 采集表的状态
		Dictionary dictionary2 = dicservice.findByDicId(1711);

		Colle colle = new Colle();
		colle.setColContent(demend);
		colle.setColCompany(company);
		colle.setColDate(new Date());
		colle.setColLinkman(linkman);
		colle.setColPhone(tel);
		colle.setColRemark(more);
		colle.setColFrom(cusfrom);
		colle.setDictionary1(dictionary1);
		colle.setDictionary2(dictionary2);
		colle.setFormsDetail(null);
		colle.setUser(user);
		boolean result = service.addCollection(colle);
		if (result) {
			mv = new ModelAndView("/getCustomer/huokexiansuo");
		} else {
			mv = new ModelAndView("/error");
		}
		return mv;
	}

	/**
	 * 进入详情页，查看单条采集信息 ty
	 */
	@GetMapping("huokeDetails")
	public ModelAndView huokeDetails(HttpServletRequest request){
		String parameter = request.getParameter("id");
		System.out.println(parameter+"采集编号");
		JSONObject colle = service.selectBycid(parameter);
		
		ModelAndView mv = new ModelAndView("/getCustomer/colleDetails");
		mv.addObject("colle", colle);
		
		return mv;
	}


	/**
	 * 访问地址http://localhost:8089/createOrder
	 * 访问制作获客html5
	 * @return
	 *
	 */
	/*@RequestMapping("/dohtml5")
	public String getCustomerdo(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		return "getCustomer/dohtml5";
	}*/
	
}
